package com.abc.easyclaim;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.abc.bean.Case;
import com.abc.bean.Editor;
import com.abc.bean.Text;
import com.abc.db.DBManager;
import com.abc.tool.Constants;
import com.abc.widget.ListViewAdapter;
import com.abc.widget.TextListViewAdapter;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class TextListActivity extends Activity implements OnClickListener{
	private static final String TAG = "TextListActivity";
	private DBManager mgr = MainApplication.getMgr();
	private ListView caseListView ;
	private TextListViewAdapter listViewAdapter;   
	private List<Map<String, Object>> listItems = new ArrayList<Map<String,Object>>();//数据
	private Integer caseId;
	
	private Integer itemid;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_text_list);
		//读取传过来的title;
		caseId = MainApplication.getCurrentCaseId();
		//填充数据
		caseListView =(ListView) findViewById(R.id.text_list_view);
		refreshList();
		Button addBtn = (Button) findViewById(R.id.text_list_add);
		addBtn.setOnClickListener(this);
		Button backBtn = (Button) findViewById(R.id.text_list_back);
		backBtn.setOnClickListener(this);
		//动态注册广播
		IntentFilter intentFilter = new IntentFilter();  
	    intentFilter.addAction(Constants.INTENT_REFRESH_TEXT_LIST);  
	    registerReceiver(mRefreshBroadcastReceiver, intentFilter); 
	    
	}

	private void refreshList() {
		listItems = getListItems(); 
		listViewAdapter = new TextListViewAdapter(this, listItems); //创建适配器   
		caseListView.setAdapter(listViewAdapter);
		/*caseListView.setOnItemClickListener(new OnItemClickListener(){ 
            @Override 
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
                    long arg3) { 
                //获得选中项的HashMap对象 
                HashMap<String,Object> map=(HashMap<String, Object>)caseListView.getItemAtPosition(arg2); 
                Intent intent = new Intent(TextListActivity.this,MetroMenuActivity.class);
                Integer para =(Integer) listItems.get(arg2).get("id");
                intent.putExtra("id", para);
                startActivity(intent);
            }              
        }); */
		//长按事件
		registerForContextMenu(caseListView);   //为列表注册上下文菜单
		caseListView.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				System.out.println(arg2);
				itemid = arg2;              //传递第几项被选中
				return false;
			}			
		});	 
	}

	private List<Map<String, Object>> getListItems() {
		listItems.clear();
        List<Text> texts = mgr.queryTexts(caseId);
		for(int i = 0; i<texts.size(); i++) {  
			Text text = texts.get(i);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", text.getId());
            map.put("title", text.getTitle());              //物品标题   
            map.put("info", "上次修改时间:"+ text.getLastModifyDate());     //物品名称   
            listItems.add(map);   
        }      
        return listItems;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.text_list, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.text_list_add:
			startActivity(new Intent(TextListActivity.this, TextAddActivity.class));
			break;
		case R.id.text_list_back:
			//创建广播
			Intent intent = new Intent();  
	        intent.setAction(Constants.INTENT_REFRESH_METRO_MENU);  
	        sendBroadcast(intent);  
			finish();
			break;
		default:
			break;
		}
	}
	
	 @Override  
    protected void onDestroy() {  
        super.onDestroy();  
        unregisterReceiver(mRefreshBroadcastReceiver);
 
    } 
	private BroadcastReceiver mRefreshBroadcastReceiver = new BroadcastReceiver() {  
		  
	      @Override  
	      public void onReceive(Context context, Intent intent) {  
	          String action = intent.getAction();  
	          if (action.equals(Constants.INTENT_REFRESH_TEXT_LIST))  
	          {  
	              refreshList();  
	          }  
	      }  
	};  
	
	//创建上下文菜单时触发此方法
    @Override
 	public void onCreateContextMenu(ContextMenu menu, View v,
 			ContextMenuInfo menuInfo) {
 		// TODO Auto-generated method stub
     	getMenuInflater().inflate(R.menu.text_popup_menu, menu);       //添加菜单布局
 		super.onCreateContextMenu(menu, v, menuInfo);
 	}
    
	//上下文菜单的菜单项被单击时触发该方法
    @Override
	public boolean onContextItemSelected(MenuItem item) {
		int menuItem = item.getItemId();
		if (menuItem == R.id.reedit) {
			Intent intent = new Intent(TextListActivity.this,TextAddActivity.class);
            intent.putExtra("isEdit", true);
            intent.putExtra("textId", (Integer)(listItems.get(itemid).get("id")));
            startActivity(intent);
		} 
		else if (menuItem == R.id.delete) {
			mgr.deleteText((Integer)(listItems.get(itemid).get("id")));
			refreshList();
		} 
		else if (menuItem == R.id.exit) {
			
		}
		return super.onContextItemSelected(item);
	}
	
           
}

